package com.ly.juc;

import lombok.*;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * @Classname TestBlockQueue
 * @Description TODO
 * @Date 2020/10/4 18:06
 * @Author 冷心影翼
 */
public class TestBlockQueue {

	public static void main(String[] args) {

	}
}

class Shop {

	private BlockingQueue<Good> goodQueue;

	public Shop() {
		goodQueue = new ArrayBlockingQueue<>(3);

	}

	public boolean productByAdd(Good good) {
		return goodQueue.add(good);
	}
	public Good consumeByRemove() {
		return goodQueue.remove();
	}

	public Boolean consumeByRemove(Good good) {
		return goodQueue.remove(good);
	}

	public boolean productByOffer(Good good) {
		return goodQueue.offer(good);
	}

	public Good consumeByPoll() {
		return goodQueue.poll();
	}

	public void productByPut(Good good) {
		try {
			goodQueue.put(good);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public Good consumeByTake() {
		Good good = null;
		try {
			good = goodQueue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return good;
	}

	public boolean productByOfferTime(Good good) {
		boolean offer = false;
		try {
			offer = goodQueue.offer(good, 1, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return offer;
	}

	public Good consumeByOfferTime() {
		Good good = null;
		try {
			good = goodQueue.poll(1, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return good;
	}

	public Good peek() {
		return goodQueue.peek();
	}

}

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
class Good {
	private String name;

}
